<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        :clickModalClose="true"
        @confirm="handleSubmit"
        @close="handleClose"
		@keypress=""
    >
      <el-form ref="formRef" :model="formData" label-width="84px" @submit.native.prevent>
        <el-form-item label="请输入密码" prop="devicePassword">
          <el-input v-model="formData.devicePassword" type="password" placeholder="请输入密码"/>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type {FormInstance} from 'element-plus'
import {effect} from '@/api/devicebysite'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type {PropType} from 'vue'

defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑设备' : '新增设备'
})

const formData = reactive({
  address:"",
  devicePassword:""
})


const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
})

const handleSubmit = async () => {
  
  await formRef.value?.validate()
  const data= await effect(formData)
  if(data.address!=null){
	feedback.msgSuccess('操作成功')
	popupRef.value?.close()
	emit('success')
  }else{
	feedback.msgError('密码错误')  
  }
}
</script>
